/**
* @author ahammer
 * @date   2009 12  new class because of ROI (graphic) functionality
 * @update  
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.util.Iterator;
import java.util.Vector;

import javax.media.jai.ROIShape;
import com.sun.media.jai.widget.DisplayJAI;

/**
 * This class extends DisplayJAI
 * The method paintComponent always displays the graphic (ROI) 
 *
 */
public class LookDisplayJAI extends DisplayJAI{
	
	private ROIShape[]         boundEditableRectangles = null;
	private ROIShape 		   currROIShape            = null;
	private Vector<ROIShape>   currROIShapeVector      = null;
    //--------------------------------------------------------------------------------------	 
	/**
	 * This method sets the current ROI shape
	 * @param ROIShape
	 */
	public void setCurrROIShape(ROIShape rs) {
		currROIShape = rs;	
	}
	/**
	 * This method gets the current ROI shape
	 * @return ROIShape
	 */
	public ROIShape getCurrROIShape() {
		return currROIShape;
		
	}
	//-------------------------------------------------------------------------------------- 
	/**
	 * This method sets the current Vector of ROIShapes
	 * @param ROIShape
	 */
	public void setROIShapeVector(Vector<ROIShape> vec) {
		//System.out.println("LookDisplayJAI: I'm here");
		currROIShapeVector = vec;
		
	}
	/**
	 * This method gets the current Vector of ROIShapes
	 * @return Vector<ROIShape>
	 */
	public  Vector<ROIShape> getROIShapeVector() {
		return currROIShapeVector;	
	}
	//-------------------------------------------------------------------------------------- 
	/**
	 * This method sets the small editable ROI rectangles
	 */
	public void setBoundEditableRectangles(ROIShape[] rs) {
		boundEditableRectangles = rs;	
	}
	/**
	 * This method gets the small editable ROI rectangles
	 * @return 
	 */
	public ROIShape[] getBoundEditableRectangles() {
		return boundEditableRectangles;	
	}
	//--------------------------------------------------------------------------------------
	 
	
	/**
	 * This methods paints the graphic and/or ROIs
	 */
	public void paintComponent(Graphics g) {  
		super.paintComponent(g);  
		Graphics2D g2 = (Graphics2D)g;  
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setColor(Color.RED);
		float zoom = Look.getLookFrame().zoom;
		AffineTransform at = new AffineTransform(zoom, 0d, 0d, zoom, 0, 0); //DisplayJAI coordinates

		//Vector<ROIShape> shapeVector = (Vector<ROIShape>)IqmTools.getROIShapeVector(); //currROIShapeVector
		Vector<ROIShape> shapeVector = currROIShapeVector;
		if (shapeVector != null){
		    Iterator itr = shapeVector.iterator();
		    while( itr.hasNext() ) {
		    	ROIShape roiShape = (ROIShape)itr.next();
		    	if (roiShape != null){
			        roiShape = new ROIShape(roiShape.transform(at).getAsShape());
					g2.draw(roiShape.getAsShape());
		    	}
		    }
		}
		
		//ROIShape roiShape = IqmTools.getROIShape();
		ROIShape roiShape = currROIShape  ;
		if ((roiShape != null) && (Look.getLookFrame().buttonEditRoi.isSelected())){
			
			roiShape = new ROIShape(roiShape.transform(at).getAsShape());
		
			Rectangle bounds = roiShape.getBounds();
			g2.setColor(Color.GREEN);			
			g2.draw(bounds);		
			Point pt1 = new Point(bounds.x,                  bounds.y);                   //oben links 
			Point pt2 = new Point(bounds.x + bounds.width/2, bounds.y);                   //oben mitte
			Point pt3 = new Point(bounds.x + bounds.width,   bounds.y);                   //oben rechts
			Point pt4 = new Point(bounds.x + bounds.width,   bounds.y + bounds.height/2); //rechts mitte
			Point pt5 = new Point(bounds.x + bounds.width,   bounds.y + bounds.height);   //unten rechts
			Point pt6 = new Point(bounds.x + bounds.width/2, bounds.y + bounds.height);   //unten mitte
			Point pt7 = new Point(bounds.x,                  bounds.y + bounds.height);   //unten links
			Point pt8 = new Point(bounds.x,   			     bounds.y + bounds.height/2); //links mitte
			Rectangle[] rectArray = new Rectangle[8];
			rectArray[0] = new Rectangle(pt1.x-3, pt1.y-3, 6, 6);
			rectArray[1] = new Rectangle(pt2.x-3, pt2.y-3, 6, 6);
			rectArray[2] = new Rectangle(pt3.x-3, pt3.y-3, 6, 6);
			rectArray[3] = new Rectangle(pt4.x-3, pt4.y-3, 6, 6);
			rectArray[4] = new Rectangle(pt5.x-3, pt5.y-3, 6, 6);
			rectArray[5] = new Rectangle(pt6.x-3, pt6.y-3, 6, 6);
			rectArray[6] = new Rectangle(pt7.x-3, pt7.y-3, 6, 6);
			rectArray[7] = new Rectangle(pt8.x-3, pt8.y-3, 6, 6);
			g2.setColor(Color.RED);
			g2.fill(rectArray[0]);
			g2.fill(rectArray[1]);
			g2.fill(rectArray[2]);
			g2.fill(rectArray[3]);
			g2.fill(rectArray[4]);
			g2.fill(rectArray[5]);
			g2.fill(rectArray[6]);
			g2.fill(rectArray[7]);
			
			//Calculate edit points in real coordinates
			ROIShape[] rectShapes = new ROIShape[8];
			for (int i = 0; i < 8; i++){
				ROIShape rectShape = new ROIShape(rectArray[i]);
				AffineTransform at2 = new AffineTransform(1d/zoom, 0d, 0d, 1d/zoom, 0, 0);
				rectShapes[i] = new ROIShape(rectShape.transform(at2).getAsShape());
				
			}
			this.setBoundEditableRectangles(rectShapes);
		}
	}


	
	
}
